package cn.maihe.elg.operation.centers.epoint.sanming.handler.service;

import cn.maihe.elg.operation.centers.epoint.sanming.dto.EpointBaseRespDTO;
import cn.maihe.elg.operation.centers.epoint.sanming.dto.handler.EpointClaimsCancelReq;
import cn.maihe.elg.operation.centers.epoint.sanming.handler.EpointCenterTradeService;
import cn.maihe.elg.operation.centers.supports.CenterTradeContext;
import cn.maihe.elg.operation.centers.supports.annotation.CenterTradeService;
import cn.maihe.elg.operation.model.enums.*;
import cn.maihe.elg.operation.repository.entity.AcceptOrderInfoDO;
import cn.maihe.elg.operation.repository.entity.ApplyOrderInfoDO;
import cn.maihe.elg.operation.repository.entity.RefundOrderInfoDO;
import cn.maihe.elg.operation.service.order.AcceptOrderInfoService;
import cn.maihe.elg.operation.service.order.ApplyOrderInfoService;
import cn.maihe.elg.operation.service.order.RefundOrderInfoService;
import cn.maihe.elg.operation.supports.system.ElgResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;

/**
 * @Description 理赔取消接口 理赔中才能发起取消，取消后同时同步理赔订单状态为取消
 * @Author caixh
 * @Date 2021/7/13.
 */
@CenterTradeService(
        provider = CenterProviderEnum.HYMAKE,
        centerNo = CenterNoEnum.SANMING_D,
        tradeType = TradeType.claimscancel,
        reqClass = EpointClaimsCancelReq.class,
        respClass = EpointBaseRespDTO.class
)
@Slf4j
public class EpointClaimsCancelService extends EpointCenterTradeService<EpointClaimsCancelReq, EpointBaseRespDTO> {

    @Resource
    AcceptOrderInfoService acceptOrderInfoService;
    @Resource
    ApplyOrderInfoService applyOrderInfoService;
    @Resource
    RefundOrderInfoService refundOrderInfoService;
    @Resource
    private ElgResourceService elgResourceService;


    @Override
    protected void verifyParam(CenterTradeContext<EpointClaimsCancelReq, EpointBaseRespDTO> context) {
        super.verifyParam(context);
        EpointClaimsCancelReq req = context.getReq();
        String orderNo = req.getApplyno();
        Assert.notNull(orderNo, () -> "业务流水号不能为空");
        Assert.notNull(req.getBaohanno(), () -> "保函编号不能为空");
        Assert.notNull(req.getApplyno(), () -> "业务流水号不能为空");
        Assert.notNull(req.getClaimsapplyno(), () -> "理赔申请编号不能为空");

    }
    /**
     * 具体业务处理
     *
     * @param context
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public EpointBaseRespDTO processService(CenterTradeContext<EpointClaimsCancelReq, EpointBaseRespDTO> context) {

        EpointClaimsCancelReq req = context.getReq();
        String guaranteeNo = req.getBaohanno();
        String centerOrderId = req.getApplyno();

        //查询申请保函记录及状态是否已出函状态
//        AcceptOrderInfoDO orderInfo = acceptOrderInfoService.getByGuaranteeNo(guaranteeNo);
//        ApplyOrderInfoDO applyOrderInfo = applyOrderInfoService.getByGuaranteeNo(guaranteeNo);

          AcceptOrderInfoDO orderInfo = acceptOrderInfoService.getByCenterOrderId(context.getCenterId(),context.getOrgId(),centerOrderId);
        ApplyOrderInfoDO applyOrderInfo = applyOrderInfoService.getByCenterOrderId(centerOrderId);

        guaranteeNo = orderInfo.getGuaranteeNo();

        Assert.notNull(orderInfo, "查无此保函编号申请记录");
        Assert.notNull(applyOrderInfo, "查无此保函编号申请记录");
        Assert.isTrue(AcceptOrderStatus.HAS_OUT_GUARANTEE.getStatus() == orderInfo.getStatus(), "此保函非出函状态不能申请理赔取消");

        ClaimsStatus claimsStatus = ClaimsStatus.resolve(orderInfo.getClaimsStatus());
        if (claimsStatus == ClaimsStatus.SUCCESS) {
            throw new RuntimeException("理赔审核已通过,不能理赔取消");
        }
        if (claimsStatus == ClaimsStatus.FAILURE) {
            throw new RuntimeException("理赔审核已拒绝,不能理赔取消");
        }
        if (claimsStatus == ClaimsStatus.CANCEL) {
            throw new RuntimeException("理赔状态已取消,不能重复理赔取消");
        }

        //待确认：理赔申请的业务流水号作为理赔取消的理赔申请编号
//        RefundOrderInfoDO refundOrderInfoDO = refundOrderInfoService.getByGuaranteeNoAndRefundOrderNo(guaranteeNo, req.getClaimsapplyno());
        RefundOrderInfoDO refundOrderInfoDO = refundOrderInfoService.getByApplyNo(req.getApplyno());
        if(refundOrderInfoDO==null){
            throw new RuntimeException("查无此保函编号的理赔申请编号记录");
        }
        refundOrderInfoDO.setStatus(RefundOrderStatus.CANCELED.getStatus());
        refundOrderInfoDO.setRemark("客户取消理赔");

        refundOrderInfoService.updateById(refundOrderInfoDO);
        AcceptOrderInfoDO updateOrderInfo = AcceptOrderInfoDO.builder().id(orderInfo.getId())
                .claimsStatus(ClaimsStatus.CANCEL.getStatus()).remark(refundOrderInfoDO.getRemark())
                .build();
        acceptOrderInfoService.updateById(updateOrderInfo);

        return EpointBaseRespDTO.success("理赔取消成功");
    }
}
